package com.Java.train.business.service.Impl;

import com.Java.train.common.response.CommonResp;
import com.Java.train.business.entity.DTO.StationDTO;
import com.Java.train.business.req.StationQueryReq;
import com.Java.train.common.exception.BusniessException;
import com.Java.train.common.exception.BusniessExceptionEnum;
import com.Java.train.common.response.PageResp;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.Java.train.business.entity.Station;
import com.Java.train.business.mapper.StationMapper;
import com.Java.train.business.service.StationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.Java.train.common.utils.SnowFlakeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import org.springframework.util.CollectionUtils;
import cn.hutool.core.date.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.Arrays;
import java.util.Collections;
/**
* 车站 服务实现类
*
* @author luomoxingchen
* @since 2024-09-22
*/
@Service
public class StationServiceImpl extends ServiceImpl<StationMapper, Station> implements StationService {

    private static final Logger LOG = LoggerFactory.getLogger(StationService.class);

    @Autowired
    StationMapper stationMapper;


    @Override
    public PageResp<StationDTO> queryList(StationQueryReq stationQueryReq){
        // 1.获取当前用户id信息
        //        Long id = LoginMemberContext.getId();
        // 2.查询数据库中是否存在当前信息（id）
        LOG.info("查询页码：{}", stationQueryReq.getPage());
        LOG.info("每页条数：{}", stationQueryReq.getSize());
        PageHelper.startPage(stationQueryReq.getPage(),stationQueryReq.getSize());
        List<Station> stations = list();
        if(stations == null || CollUtil.isEmpty(stations)){
            // 返回空列表
            return new PageResp<StationDTO>();
//            throw new BusniessException(BusniessExceptionEnum.BUSINESS_STATION_NOT_EXITS);
        }

        // 3 分析分页信息
        // 3.1 获取分页总条数和页数
        PageInfo<Station> pageInfo = new PageInfo<>(stations);
        LOG.info("总行数：{}", pageInfo.getTotal());
        LOG.info("总页数：{}", pageInfo.getPages());

        // 3.2 转为封装后的返回值并封装到分页返回值中
        List<StationDTO> list = BeanUtil.copyToList(stations, StationDTO.class);
        PageResp<StationDTO> pageResp = new PageResp<>();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(list);
        // 4.返回查询到的信息
        return pageResp;
    }

    @Override
    public CommonResp<Object> modify(Station station) {
        Long id = station.getId();
        DateTime dateTime = DateTime.now();
        if(ObjectUtil.isNull(id)){
            // 查询是否已经存在过重复站点
            String name = station.getName();
            Station one = getStationByUnique(name);
            if(ObjectUtil.isNotNull(one)|| ObjectUtil.isNotEmpty(one)){
                throw new BusniessException(BusniessExceptionEnum.BUSINESS_STATION_ALREADY_EXITS);
            }

            station.setId(SnowFlakeUtil.getSnowFlakeNextId());
            station.setCreateTime(dateTime);
            station.setUpdateTime(dateTime);
            boolean save = save(station);
            return new CommonResp<>(save);
        } else {
            station.setUpdateTime(dateTime);
            boolean update = updateById(station);
            return new CommonResp<>(update);
        }
    }

    @Override
    public CommonResp<Object> remove(String ids) {
        if(StrUtil.isNotEmpty(ids)){
            String[] array = ids.split(",");
            if(!CollectionUtils.isEmpty(Arrays.asList(array))) {
                return new CommonResp<>(stationMapper.deleteBatchIds(Arrays.asList(array)));
            }
        }
        return new CommonResp<>(Collections.EMPTY_LIST);
    }

    @Override
    public List<StationDTO> queryAll() {
        // 1.获取station列表
        List<Station> stations = list();
        if(stations == null || CollUtil.isEmpty(stations)){
            throw new BusniessException(BusniessExceptionEnum.BUSINESS_STATION_NOT_EXITS);
        }
        // 2 转为DTO数据
        List<StationDTO> list = BeanUtil.copyToList(stations, StationDTO.class);
        // 3.返回查询到的信息
        return list;
    }

    // 通过唯一键校验查重
    private Station getStationByUnique(String name) {
        LambdaQueryWrapper<Station> wrapper = new LambdaQueryWrapper<>();
        Station one = getOne(wrapper.eq(Station::getName, name));
        return one;
    }

}


